<?php
require_once('gbif.occurrence.php');
class Occurrence1 extends Occurrence {

	function __construct( $file = NULL ) {
		parent::__construct($file);
	}

	public function validateOccurrenceData( $sheetIndex = 2 ) {
		if (!$this->fileOpen) $this->openFile();

		$passed = true;
		$wk = $this->objPHPExcel->setActiveSheetIndex( $sheetIndex );

		$highestRow = $wk->getHighestRow();
		$highestColumn = $wk->getHighestColumn();
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

		$rowIndex = -1;
		# Builds array but removes any blank rows
		foreach ($wk->getRowIterator() as $row) {
			$rowIndex++;
			$blankRow = true;

			$cellIterator = $row->getCellIterator();
 			$cellIterator->setIterateOnlyExistingCells(false);
#			$cellIterator->setIterateOnlyExistingCells(true);
			$colIndex = -1;

			foreach ($cellIterator as $cell) {
				$colIndex++;
				$value = $cell->getValue(); // Removed utf8_decode() for unicode compatibility. bko@GBIFS on 12 Dec 11.

				# Check that data do not contain control characters that break the XML transfer format (e.g. vertical tabs).  Remove if included.
				$value = str_replace("\x0B", '', $value);  //remove all vertical tabs in sender string
#ADD MORE CONDITIONS
				
				if ($colIndex == 18 || $colIndex == 34 || $colIndex == 44) { // Transform date format for column "dateIdentified," "eventDate," "modified."
					$this->occurrenceArray[$rowIndex][$colIndex] = date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP(trim($value)));
				} else {
					$this->occurrenceArray[$rowIndex][$colIndex] = trim($value);
				}
								
				if ($this->occurrenceArray[$rowIndex][$colIndex] != '') {
					$blankRow = false;
				}
			}

			if ($blankRow || $rowIndex == 0) {
				// Clean blank empty rows
				unset($this->occurrenceArray[$rowIndex]);
/*				
			} else {
				print "<pre>";
				print_r( $this->occurrenceArray[$rowIndex] );
*/				
			}
		}
		return( $passed );
	}

	public function writeOccurrenceData($path = '') {
		$fp = fopen($path . $this->occurrenceFilename, 'w');
		fputs($fp, "\xEF\xBB\xBF"); 
		fputcsv_force($fp, $this->occurrenceOutputHeaderList);
		
		if(is_array($this->occurrenceArray)) {
		foreach($this->occurrenceArray as $rec) {
			fputcsv_force($fp, $rec);
		}
		}
			
		fclose($fp);
	}

}
?>